From e55a988eb67035fa1723c8de4a8125c466255511 Mon Sep 17 00:00:00 2001 From: robertl Date: Sun, 21 Dec 2003 20:30:54 +0000 Subject: [PATCH] Replace old endianness logic in binary formats with newer stuff. Fix loops that free ll elements. Misc cleanups. --- gpsbabel/duplicate.c | 2 +- gpsbabel/garmin_tables.h | 1 - gpsbabel/main.c | 1 - gpsbabel/mapsend.c | 104 ++++++--------------------------------- gpsbabel/mapsource.c | 2 +- gpsbabel/navicache.c | 5 -- gpsbabel/polygon.c | 1 - gpsbabel/psp.c | 83 ++----------------------------- gpsbabel/route.c | 15 ++---- gpsbabel/tpg.c | 52 ++------------------ gpsbabel/waypt.c | 18 +++---- 11 files changed, 35 insertions(+), 249 deletions(-) diff --git a/gpsbabel/duplicate.c b/gpsbabel/duplicate.c index a7846e40b..e9213ad5d 100644 --- a/gpsbabel/duplicate.c +++ b/gpsbabel/duplicate.c @@ -109,7 +109,7 @@ get_crc32(void * data, int datalen) static btree_node * addnode (btree_node * tree, btree_node * newnode, btree_node **oldnode) { - btree_node * tmp, * last; + btree_node * tmp, * last = NULL; if ( *oldnode ) {*oldnode = NULL;} diff --git a/gpsbabel/garmin_tables.h b/gpsbabel/garmin_tables.h index ada2a8bdc..494397260 100644 --- a/gpsbabel/garmin_tables.h +++ b/gpsbabel/garmin_tables.h @@ -35,7 +35,6 @@ extern int mps_find_icon_number_from_desc(const char *desc, garmin_formats_e garmin_format); - /* MapSource 4.13 */ static icon_mapping_t icon_table[] = { /* mps pcx desc */ diff --git a/gpsbabel/main.c b/gpsbabel/main.c index 306424330..82ec89d89 100644 --- a/gpsbabel/main.c +++ b/gpsbabel/main.c @@ -218,7 +218,6 @@ main(int argc, char *argv[]) usage(argv[0]); exit(0); } - if (ovecs == NULL) waypt_disp_all(waypt_disp); diff --git a/gpsbabel/mapsend.c b/gpsbabel/mapsend.c index e9e1f2ff8..798455441 100644 --- a/gpsbabel/mapsend.c +++ b/gpsbabel/mapsend.c @@ -29,30 +29,12 @@ static FILE *mapsend_file_in; static FILE *mapsend_file_out; static void *mkshort_handle; -static int endianness_tested; -static int i_am_little_endian; - static int route_wp_count; static int mapsend_infile_version; static int trk_version = 30; #define MYNAME "mapsend" -static void -test_endianness(void) -{ - union { - long l; - unsigned char uc[sizeof (long)]; - } u; - - u.l = 1; - i_am_little_endian = u.uc[0]; - - endianness_tested = 1; - -} - static void mapsend_rd_init(const char *fname) { @@ -69,103 +51,45 @@ mapsend_rd_deinit(void) } static -size_t +void my_fread8(void *ptr, FILE *stream) { unsigned char cbuf[8]; - unsigned char *cptr = ptr; size_t rv; - if (!endianness_tested) { - test_endianness(); - } - - if (i_am_little_endian) { - rv = fread(ptr, 8, 1, stream); - } else { - rv = fread(cbuf, 8, 1, stream); - cptr[0] = cbuf[7]; - cptr[1] = cbuf[6]; - cptr[2] = cbuf[5]; - cptr[3] = cbuf[4]; - cptr[4] = cbuf[3]; - cptr[5] = cbuf[2]; - cptr[6] = cbuf[1]; - cptr[7] = cbuf[0]; - } - return rv; + rv = fread(cbuf, 8, 1, stream); + le_read64(ptr, cbuf); + } static -size_t +void my_fwrite8(void *ptr, FILE *stream) { unsigned char cbuf[8]; - unsigned char *cptr = ptr; - if (!endianness_tested) { - test_endianness(); - } - - if (i_am_little_endian) { - return fwrite(ptr, 8, 1, stream); - } else { - cbuf[0] = cptr[7]; - cbuf[1] = cptr[6]; - cbuf[2] = cptr[5]; - cbuf[3] = cptr[4]; - cbuf[4] = cptr[3]; - cbuf[5] = cptr[2]; - cbuf[6] = cptr[1]; - cbuf[7] = cptr[0]; - return fwrite(cbuf, 8, 1, stream); - } + le_read64(cbuf, ptr); + fwrite(cbuf, 8, 1, stream); } static -size_t +void my_fread4(void *ptr, FILE *stream) { + unsigned int *iptr = ptr; unsigned char cbuf[4]; - unsigned char *cptr = ptr; size_t rv; - - if (!endianness_tested) { - test_endianness(); - } - if (i_am_little_endian) { - rv = fread(ptr, 4, 1, stream); - } else { - rv = fread(cbuf, 4, 1, stream); - cptr[0] = cbuf[3]; - cptr[1] = cbuf[2]; - cptr[2] = cbuf[1]; - cptr[3] = cbuf[0]; - } - return rv; + rv = fread(cbuf, 4, 1, stream); + *iptr = le_read32(cbuf); } static size_t my_fwrite4(int *ptr, FILE *stream) { - unsigned char cbuf[4]; - unsigned char *cptr = (unsigned char *) ptr; - - if (!endianness_tested) { - test_endianness(); - } - - if (i_am_little_endian) { - return fwrite(ptr, 4, 1, stream); - } else { - cbuf[0] = cptr[3]; - cbuf[1] = cptr[2]; - cbuf[2] = cptr[1]; - cbuf[3] = cptr[0]; - return fwrite(cbuf, 4, 1, stream); - } + int i = le_read32(ptr); + return fwrite(&i, 4, 1, stream); } static void @@ -528,7 +452,7 @@ void mapsend_track_hdr(const route_head * trk) * we write mapsend v3.0 tracks as mapsend v2.0 tracks get * tremendously out of whack time/date wise. */ - char *verstring; + char *verstring = "30"; queue *elem, *tmp; char *tname; unsigned char c; diff --git a/gpsbabel/mapsource.c b/gpsbabel/mapsource.c index 43606ea5c..3f015f91c 100644 --- a/gpsbabel/mapsource.c +++ b/gpsbabel/mapsource.c @@ -1499,7 +1499,7 @@ mps_write(void) char recType; int reclen; int reclen2; - int tocopy; + unsigned int tocopy; unsigned char copybuf[8192]; diff --git a/gpsbabel/navicache.c b/gpsbabel/navicache.c index 127d9c885..c70321d3c 100644 --- a/gpsbabel/navicache.c +++ b/gpsbabel/navicache.c @@ -205,11 +205,6 @@ nav_wr_deinit(void) fclose(ofd); } -static void -nav_waypt_pr(const waypoint *waypointp) -{ -} - void nav_write(void) { diff --git a/gpsbabel/polygon.c b/gpsbabel/polygon.c index 2cc00b020..46b27a31f 100644 --- a/gpsbabel/polygon.c +++ b/gpsbabel/polygon.c @@ -24,7 +24,6 @@ extern queue waypt_head; -static double pos_dist; static char *polyfileopt = NULL; static char *exclopt = NULL; diff --git a/gpsbabel/psp.c b/gpsbabel/psp.c index 4aacd5cd9..acc735973 100644 --- a/gpsbabel/psp.c +++ b/gpsbabel/psp.c @@ -23,10 +23,6 @@ #include "defs.h" #include -#ifndef M_PI -# define M_PI 3.14159265358979323846 -#endif - #define MYNAME "PSP" #define MAXPSPSTRINGSIZE 256 @@ -36,24 +32,6 @@ static FILE *psp_file_in; static FILE *psp_file_out; static FILE *mkshort_handle; -static int i_am_little_endian; -static int endianness_tested; - -static void -test_endianness(void) -{ - union { - long l; - unsigned char uc[sizeof (long)]; - } u; - - u.l = 1; - i_am_little_endian = u.uc[0]; - - endianness_tested = 1; - -} - static int psp_fread(void *buff, size_t size, size_t members, FILE * fp) { @@ -74,23 +52,9 @@ psp_fread_double(FILE *fp) unsigned char buf[8]; unsigned char sbuf[8]; - if (!endianness_tested) { - test_endianness(); - } - - psp_fread(buf, 1, 8, psp_file_in); - if (i_am_little_endian) { - return *(double *) buf; - } - sbuf[0] = buf[7]; - sbuf[1] = buf[6]; - sbuf[2] = buf[5]; - sbuf[3] = buf[4]; - sbuf[4] = buf[3]; - sbuf[5] = buf[2]; - sbuf[6] = buf[1]; - sbuf[7] = buf[0]; - return *(double *)sbuf; + psp_fread(buf, 1, 8, fp); + le_read64(sbuf, buf); + return *(double *) sbuf; } static void @@ -99,46 +63,9 @@ psp_fwrite_double(double x, FILE *fp) unsigned char *cptr = (unsigned char *)&x; unsigned char cbuf[8]; - if (!endianness_tested) { - test_endianness(); - } - if (i_am_little_endian) { - fwrite(&x, 8, 1, fp); - } else { - cbuf[0] = cptr[7]; - cbuf[1] = cptr[6]; - cbuf[2] = cptr[5]; - cbuf[3] = cptr[4]; - cbuf[4] = cptr[3]; - cbuf[5] = cptr[2]; - cbuf[6] = cptr[1]; - cbuf[7] = cptr[0]; - fwrite(cbuf, 8, 1, fp); - } - + le_read64(cbuf, cptr); + fwrite(cbuf, 8, 1, fp); } -#if 0 -static void -psp_fwrite_word(unsigned int x, FILE *fp) -{ - char *cptr = &x; - char *cbuf[4]; - - if (!endianness_tested) { - test_endianness(); - } - if (i_am_little_endian) { - fwrite(&x, 4, 1, fp); - } else { - cbuf[0] = cptr[3]; - cbuf[1] = cptr[2]; - cbuf[2] = cptr[1]; - cbuf[3] = cptr[0]; - fwrite(cbuf, 4, 1, fp); - } -} -#endif - /* Implement the grid in ascii art... This makes a bit of sense if you stand on a point over the north pole and look down on the earth. diff --git a/gpsbabel/route.c b/gpsbabel/route.c index 74cfd57a0..6c85a3bf8 100644 --- a/gpsbabel/route.c +++ b/gpsbabel/route.c @@ -134,19 +134,14 @@ void route_flush(queue *head) { queue *elem, *tmp; - route_head *last = NULL; - + queue *q; + QUEUE_FOR_EACH(head, elem, tmp) { - if ( last ) { - route_free(last); - } - last = (route_head *)elem; - } - if ( last ) { - route_free(last); + q = dequeue(elem); + route_free((route_head *) q); } - QUEUE_INIT(head); } + void route_flush_all() { diff --git a/gpsbabel/tpg.c b/gpsbabel/tpg.c index 165d2809e..ef2fbc622 100644 --- a/gpsbabel/tpg.c +++ b/gpsbabel/tpg.c @@ -32,25 +32,8 @@ static FILE *tpg_file_in; static FILE *tpg_file_out; static void *mkshort_handle; -static int i_am_little_endian; -static int endianness_tested; static unsigned int waypt_out_count; -static void -test_endianness(void) -{ - union { - long l; - unsigned char uc[sizeof (long)]; - } u; - - u.l = 1; - i_am_little_endian = u.uc[0]; - - endianness_tested = 1; - -} - static int tpg_fread(void *buff, size_t size, size_t members, FILE * fp) { @@ -71,22 +54,8 @@ tpg_fread_double(FILE *fp) unsigned char buf[8]; unsigned char sbuf[8]; - if (!endianness_tested) { - test_endianness(); - } - tpg_fread(buf, 1, 8, fp); - if (i_am_little_endian) { - return *(double *) buf; - } - sbuf[0] = buf[7]; - sbuf[1] = buf[6]; - sbuf[2] = buf[5]; - sbuf[3] = buf[4]; - sbuf[4] = buf[3]; - sbuf[5] = buf[2]; - sbuf[6] = buf[1]; - sbuf[7] = buf[0]; + le_read64(sbuf, buf); return *(double *)sbuf; } @@ -96,23 +65,8 @@ tpg_fwrite_double(double x, FILE *fp) unsigned char *cptr = (unsigned char *)&x; unsigned char cbuf[8]; - if (!endianness_tested) { - test_endianness(); - } - if (i_am_little_endian) { - fwrite(&x, 8, 1, fp); - } else { - cbuf[0] = cptr[7]; - cbuf[1] = cptr[6]; - cbuf[2] = cptr[5]; - cbuf[3] = cptr[4]; - cbuf[4] = cptr[3]; - cbuf[5] = cptr[2]; - cbuf[6] = cptr[1]; - cbuf[7] = cptr[0]; - fwrite(cbuf, 8, 1, fp); - } - + le_read64(cbuf, cptr); + fwrite(cbuf, 8, 1, fp); } static int diff --git a/gpsbabel/waypt.c b/gpsbabel/waypt.c index 54c6267f7..879a291f9 100644 --- a/gpsbabel/waypt.c +++ b/gpsbabel/waypt.c @@ -53,6 +53,7 @@ waypt_dupe(waypoint *wpt) return tmp; } + void waypt_add(waypoint *wpt) { @@ -190,21 +191,14 @@ void waypt_flush( queue *head ) { queue *elem, *tmp; - waypoint *last = NULL; - + + queue *q; QUEUE_FOR_EACH(head, elem, tmp) { - if ( last ) { - waypt_free(last); - } - last = (waypoint *)elem; - } - - if ( last ) { - waypt_free(last); + q = dequeue(elem); + waypt_free((waypoint *) q); } - - QUEUE_INIT(head); } + void waypt_flush_all() { -- 2.30.2